<template>
	<div>
		<el-form ref="configForm" :model="form" :rules="rules" label-position="top">
			<div class="base-container">
				<div class="flex items-center justify-between">
					<p class="font-bold">证书申请文件下载</p>

					<el-button type="primary" @click="handleDownload">下载</el-button>
				</div>
			</div>
		</el-form>

		<div class="page-footer">
			<el-button @click="onCancel">上一步</el-button>
			<el-button type="primary" @click="onConfirm">下一步</el-button>
		</div>
	</div>
</template>

<script setup>
const { proxy } = getCurrentInstance()

const route = useRoute()
const emits = defineEmits(['cancel', 'confirm'])

const flowId = computed(() => {
	return Number(route.query.id) || null
})

const requestNum = computed(() => {
	return route.query.num || null
})

const date = computed(() => {
	return route.query.date || null
})

const configForm = ref()
const form = ref({})
const rules = {}

const isDownload = ref(false)

function generateFileName(serialNumber, expirationDate) {
	const prefix = 'JT03875370'
	const suffix = '.IGNP'
	return `${prefix}${serialNumber}${expirationDate}${suffix}`
}

function handleDownload() {
	isDownload.value = true
	proxy.download(
		'bus/codeManagementConfig/cardPubKeyCaRequestFileDownload',
		{
			fileName: generateFileName(requestNum.value, date.value),
			id: flowId.value,
			delete: false
		},
		generateFileName(requestNum.value, date.value)
	)
}

const onCancel = () => {
	emits('cancel')
}

const onConfirm = () => {
	if (isDownload.value) {
		emits('confirm')
	} else {
		proxy.$modal.msgError('请下载证书申请文件')
	}
}
</script>

<style lang="scss" scoped>
@import './base.scss';
</style>
